package com.weiqiao.mall.service.impl;

import com.google.gson.Gson;
import com.mingsoft.util.StringUtil;
import com.weiqiao.mall.domain.StrategyEntity;
import com.weiqiao.mall.repository.IStrategyRepository;
import com.weiqiao.mall.service.IStrategyService;
import com.weiqiao.mall.service.vo.StrategyVO;
import com.weiqiao.utils.DateUtils;
import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang.StringUtils;
import org.bson.Document;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("StrategyServiceImpl")
public class StrategyServiceImpl implements IStrategyService {

    @Autowired
    private IStrategyRepository strategy;

    @Override
    public List<StrategyEntity> strategyList(StrategyVO vo){
        //Bson filter, Bson sort, int skip, int limit, int company_id
        Document doc = new Document(); // 查询条件
        Document sort = new Document(); // 排序
        doc.append("delete_flag",0);
        if(!StringUtils.isBlank(vo.getTitle())){
            doc.append("tittle",new Document("$regex",vo.getTitle()));
        }
        sort.append("sort",1);
        int pagesize = vo.getSize(); // 多少个
        int pagenum = vo.getPage() * pagesize; // 页数
        List<String> list = strategy.strategyList(doc,sort,pagenum,pagesize);
        List<StrategyEntity> result = new ArrayList<>();
        SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
        Gson gson = new Gson();
        list.forEach(s -> {
            StrategyEntity Entity = gson.fromJson(s,StrategyEntity.class);
            String time = Entity.getCreate_date();
            long create_time = Long.parseLong(time);
            Entity.setCreate_date(sd.format(new Date(create_time)));
            result.add(Entity);
        });
        return  result;
    }

    @Override
    public StrategyEntity strategyInfo(int companyId, String strategy_id) {
        String info = strategy.getStrategyInfo(new Document("strategy_id", strategy_id));
        if (StringUtil.isBlank(info)) return new StrategyEntity();
        return new Gson().fromJson(info, StrategyEntity.class);
    }

    @Override
    public Map<String, Object> updateStrategy(StrategyVO vo){
        Document bson = new Document(); //条件
        Document doc = new Document(); // 内容
        Map<String,Object> map = new HashedMap();
        if(StringUtils.isBlank(vo.getStrategy_id())){
            map.put("result",false);
            map.put("resultmsg", "攻略Id不能为空");
            return map;
        }
        bson.append("strategy_id",vo.getStrategy_id());
        String time = DateUtils.currentTimeMillis();
        doc.append("update_date",time);
        doc.append("company_id",vo.getCompany_id());
        doc.append("strategy_img",vo.getStrategy_img());
        doc.append("title",vo.getTitle());
        doc.append("details",vo.getDetails());
        Boolean lean = strategy.updateStrategy(bson ,doc);
        if(lean){
            map.put("result",true);
            map.put("resultmsg", "修改成功");
        }else{
            map.put("result",false);
            map.put("resultmsg", "修改失败");
        }
        return  map;
    }

    @Override
    public Map<String, Object> insertStrategy(StrategyEntity vo){
        Map<String,Object> map = new HashedMap();
        String time = DateUtils.currentTimeMillis();
        vo.setCreate_date(time);
        vo.setStrategy_id(time);
        String json = new Gson().toJson(vo);
        Boolean lean = strategy.insertStrategy(json);
        if(lean){
            map.put("result",true);
            map.put("resultmsg", "添加成功");
        }else{
            map.put("result",false);
            map.put("resultmsg", "添加失败");
        }
        return  map;
    }

    @Override
    public Map<String, Object> deleteStrategy(StrategyVO vo){
        Map<String,Object> map = new HashedMap();
        Document filter = new Document();
        Document doc = new Document();
        if(StringUtils.isBlank(vo.getStrategy_id())){
            map.put("result",false);
            map.put("resultmsg", "攻略Id不能为空");
            return  map;
        }
        filter.append("strategy_id",vo.getStrategy_id());
        doc.append("delete_flag",1);
        Boolean lean = strategy.deleteStrategy(filter,doc);
        if(lean){
            map.put("result",true);
            map.put("resultmsg", "删除成功");
        }else{
            map.put("result",false);
            map.put("resultmsg", "删除失败");
        }
        return  map;
    }

    @Override
    public int countStrategy(StrategyVO vo){
        Document doc = new Document();
        doc.append("delete_flag", 0);
        return  strategy.countStrategy(doc);
    }
}
